import { APPEND_LIST, PREPEND_LIST } from './../mutation-type'
import { getGalleryPerview } from './../../service/getData'

export default {
  async initPreviewList ({ commit, state }, {gid, token, pageNo}) {
    state.loadMoreDisabled = true
    state.data = []
    state.showLoading = true
    state.gid = gid
    state.token = token
    try {
      let result = await getGalleryPerview(gid, token, pageNo)
      state.startPage = pageNo
      state.showLoading = false
      setTimeout(function () {
        state.loadMoreDisabled = false
      }, 1000)
      state.pageNumber = result.payload.pageNumber
      state.page = result.payload.page
      state.pageLength = result.payload.pageLength
      state.data = result.payload.data
    } catch (e) {
      commit('SET_SNACKMESSAGE', e)
    }
  },
  async previewLoadMore ({ commit, state }) {
    if (state.loadMore === true || state.loadMoreDisabled === true) {
      return
    }
    state.loadMore = true
    try {
      let result = await getGalleryPerview(state.gid, state.token, state.page + 1)
      state.loadMore = false
      state.page += 1
      commit(APPEND_LIST, {params: 'previewList', data: result.payload.data})
      if (result.payload.data.length < 20) {
        state.loadMoreDisabled = true
        return
      }
      state.loadMore = false
    } catch (e) {
      commit('SET_SNACKMESSAGE', e)
    }
  },
  async previewLoadTop ({ commit, state }, refs) {
    if (state.startPage === 1) {
      commit('SET_SNACKMESSAGE', '已经是第一页了哦')
      return
    }
    let result = await getGalleryPerview(state.gid, state.token, state.startPage - 1)
    state.startPage -= 1
    commit(PREPEND_LIST, {params: 'previewList', data: result.payload.data})
    refs.loadmore.onTopLoaded()
  }
}
